package com.dys.hejiayuncommunity.community.service.impl;

import com.dys.hejiayuncommunity.community.domain.HjyCommunity;
import com.dys.hejiayuncommunity.community.domain.dto.HjyCommunityDTO;
import com.dys.hejiayuncommunity.community.domain.vo.HjyCommunityVO;
import com.dys.hejiayuncommunity.community.mapper.HjyCommunityMapper;
import com.dys.hejiayuncommunity.community.service.HjyCommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @className: com.dys.hejiayuncommunity.community.service.impl.HjyCommunityServiceImpl
 * @description: com.dys.hejiayuncommunity.community.service.impl
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/12/03 22:45
 */
@Service
public class HjyCommunityServiceImpl implements HjyCommunityService {
    private static final String CODE_PREFIX = "COMMUNITY_";
    @Autowired
    private HjyCommunityMapper hjyCommunityMapper;

    @Override
    public List<HjyCommunityDTO> findCommunityByCondition(HjyCommunity hjyCommunity) {
        return hjyCommunityMapper.selectCommunityByCondition(hjyCommunity);
    }

    @Override
    public int addCommunity(HjyCommunity hjyCommunity) {
        // 设置小区编码
        hjyCommunity.setCommunityCode(CODE_PREFIX + System.currentTimeMillis());
        return hjyCommunityMapper.insert(hjyCommunity);
    }

    @Override
    public HjyCommunity findCommunityInfoById(Long communityId) {
        return hjyCommunityMapper.selectById(communityId);
    }

    @Override
    public int updateCommunityById(HjyCommunity hjyCommunity) {
        return hjyCommunityMapper.updateById(hjyCommunity);
    }

    @Override
    public int deleteBatchCommunityByIds(Long[] communityIds) {
        return hjyCommunityMapper.deleteBatchIds(Arrays.asList(communityIds));
    }

    @Override
    public List<HjyCommunityVO> dropDownSelectedArea(HjyCommunity hjyCommunity) {
        List<HjyCommunityDTO> dtoList = hjyCommunityMapper.selectCommunityByCondition(hjyCommunity);
        List<HjyCommunityVO> voList = dtoList.stream().map(dto -> {
            HjyCommunityVO vo = new HjyCommunityVO();
            vo.setCommunityId(dto.getCommunityId());
            vo.setCommunityName(dto.getCommunityName());
            return vo;
        }).collect(Collectors.toList());
        return voList;
    }
}
